﻿using System;
using System.Collections.Generic;
using ZWFrameWork;

public class GameInfo
{
    /// <summary>
    /// roomDic
    /// </summary>
    private Dictionary<string, CMD_CS_C_RegisterServer>
        roomTypeGold = new Dictionary<string, CMD_CS_C_RegisterServer>();

    private Dictionary<string, CMD_CS_C_RegisterServer>
        roomTypeScore = new Dictionary<string, CMD_CS_C_RegisterServer>();

    private Dictionary<string, CMD_CS_C_RegisterServer>
        roomTypeMatch = new Dictionary<string, CMD_CS_C_RegisterServer>();

    private Dictionary<string, CMD_CS_C_RegisterServer>
        roomTypeEducate = new Dictionary<string, CMD_CS_C_RegisterServer>();

    private Dictionary<string, CMD_CS_C_RegisterServer>
        roomTypePersonal = new Dictionary<string, CMD_CS_C_RegisterServer>();

    private Dictionary<ushort, tagU3DGameKind>
        gameDic = new Dictionary<ushort, tagU3DGameKind>();

    /// <summary>
    /// AddGameDataInDic
    /// </summary>
    public void AddGameDataInDic(tagU3DGameKind tag)
    {
        try
        {
            GameUtilty.Info("添加{0},{1}",tag.wKindID,tag.szKindName);
            if (!gameDic.TryAdd(tag.wKindID, tag))
            {
                GameUtilty.Error("{0}已存在", tag.wKindID);
            }
        }
        catch (Exception e)
        {
            GameUtilty.Error("{0}=error",e.Message);
        }
    }

    /// <summary>
    /// GetGameDicSize
    /// </summary>
    /// <returns></returns>
    public int GetGameDicSize()
    {
        return gameDic.Count;
    }


    /// <summary>
    /// 加入房间信息
    /// </summary>
    public void AddRoomDataInDic(CMD_CS_C_RegisterServer cmd)
    {
        switch (cmd.wServerType)
        {
            case Protocal.GAME_GENRE_GOLD:
                AddToRoomData(ref roomTypeGold, cmd);
                break;
            case Protocal.GAME_GENRE_MATCH:
                AddToRoomData(ref roomTypeMatch, cmd);
                break;
            case Protocal.GAME_GENRE_SCORE:
                AddToRoomData(ref roomTypeScore, cmd);
                break;
            case Protocal.GAME_GENRE_EDUCATE:
                AddToRoomData(ref roomTypeEducate, cmd);
                break;
            case Protocal.GAME_GENRE_PERSONAL:
                AddToRoomData(ref roomTypePersonal, cmd);
                break;
            default:
                GameUtilty.Error("{0}未知房间类型");
                break;
        }
    }

    /// <summary>
    /// 将房间信息加入字典中
    /// </summary>
    /// <param name="dic"></param>
    /// <param name="cmd"></param>
    private void AddToRoomData(ref Dictionary<string, CMD_CS_C_RegisterServer> dic, CMD_CS_C_RegisterServer cmd)
    {
        try
        {
            if (!dic.TryAdd(cmd.szServerName, cmd))
            {
                GameUtilty.Error("{0}已存在", cmd.szServerName);
            }
        }
        catch (Exception e)
        {
            GameUtilty.Error("{0}==error",e.Message);
        }
    }


    /// <summary>
    /// GetRoomDataInDic
    /// </summary>
    /// <returns></returns>
    public CMD_CS_C_RegisterServer? GetRoomDataInDic(string szServerName)
    {
        CMD_CS_C_RegisterServer cmd;
        if (roomTypeGold.TryGetValue(szServerName, out cmd))
        {
            return cmd;
        }

        if (roomTypeMatch.TryGetValue(szServerName, out cmd))
        {
            return cmd;
        }

        if (roomTypeEducate.TryGetValue(szServerName, out cmd))
        {
            return cmd;
        }

        if (roomTypeScore.TryGetValue(szServerName, out cmd))
        {
            return cmd;
        }

        if (roomTypePersonal.TryGetValue(szServerName, out cmd))
        {
            return cmd;
        }


        GameUtilty.Error("未找到");
        return null;
    }
    
}